<?php

namespace app\api\controller\wanlshop;

use addons\wanlshop\library\WanlSdk\Common;
use app\api\model\wanlshop\LifeEnd as WanlshopLifeEnd;
use app\api\model\wanlshop\Order;
use app\api\model\wanlshop\OrderGoods;
use app\api\model\wanlshop\Withdraw;
use app\common\controller\Api;
use app\common\library\token\driver\Redis;
use fast\Tree;
use think\Db;
use think\Log;


class LifeEnd extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function info()
    {
        $user = $this->auth->getUser();


        // 根据用户id 查看
        $le = WanlshopLifeEnd::where([
            'user_id' => $user->id,
        ])->find();

        if (!$le) {
            $le = new WanlshopLifeEnd();
            // 订单总金额
            $actual_payment = Order::alias('o')->join("wanlshop_pay p", "p.order_id = o.id", "LEFT")->where([
                'o.user_id' => $user->id,
                'o.state' => ['in', ['2', '3', '4', '6']],
                'p.pay_state' => '1',
                // 'o.order_no' => '1161500025250172'
                //订单状态:1=待支付,2=待发货,3=待收货,4=待评论,5=售后订单(已退款),6=已完成,7=已取消,8-待审核,9-已拒绝,10-退款中
            ])->sum('p.actual_payment');

            // 换购订单金额
            $change_payment = OrderGoods::where([
                'user_id' => $user->id,
                'type' => 'old',
                'audit_status' => 'pass'
            ])->where(function ($query) {
                $query->where('is_buyback', '1')->whereOr('is_withdraw', '1');
                return $query;
            })->sum('price');


            // 换购提现金额
            $withdrawal_price = Withdraw::alias('w')
                ->join('withdrawal_cash wc', 'wc.w_id = w.id', 'LEFT')
                ->where([
                    'w.user_id' => $user->id,
                    'wc.type' => 1,
                    'w.status' => 'successed'
                ])->sum('w.money');

            $end = bcsub($actual_payment, bcadd($change_payment, $withdrawal_price, 2), 2);
            $le->data = [
                '总订单金额' => $actual_payment,
                '已置换_已提现总金额' => bcadd($change_payment, $withdrawal_price, 2),
                '结余金额' => $end,
                '到手金额' => bcdiv($end, 2.2, 2)
            ];

        }

        return $this->success('', $le);
        // dd($actual_payment, $change_payment, $withdrawal_price);
    }

    public function apply()
    {
        $user = $this->auth->getUser();
        $params = $this->request->param();

        $id = $params['id'] ?: null;
        $le = WanlshopLifeEnd::where([
            'user_id' => $user->id
        ])->find();
        if (!$id) {
            if ($le) return $this->error('请勿重复申请', $le);
            $le = new WanlshopLifeEnd();
            $le->user_id = $user->id;

        } else {
            if ($le->id != $id) return $this->error('请稍候重试');

        }
        $le->status = 'created';
        $le->data = $params['data'];
        $result = $le->save();
        if ($result === false) return $this->error('请稍候重试');
        return $this->success('操作成功', $le);
    }
}
